package com.feicheng.common.advice;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feicheng.common.constant.Constant;
import com.feicheng.persistence.entity.Store;
import com.feicheng.persistence.service.IStoreService;
import com.feicheng.service.IFStoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
@Slf4j
public class InitAppListener implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    IStoreService storeService;

    @Autowired
    IFStoreService fStoreService;

    @Override
    @Transactional
    public void onApplicationEvent(ContextRefreshedEvent event) {
        QueryWrapper<Store> storeQueryWrapper = new QueryWrapper<Store>();
        storeQueryWrapper.eq(Constant.ENTITY.STORE.STATU, true);
        storeQueryWrapper.eq(Constant.ENTITY.STORE.D_FLG, false);

        List<Store> storeList = storeService.list(storeQueryWrapper);

        for (Store store : storeList) {
            try {
                fStoreService.openStore(store.getStoreId());
            } catch (Exception e) {
                continue;
            }
        }


    }
}
